﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.UI;

namespace Doo.WebUI.SiteBuilder.PropertyEditor
{
    /// <summary>
    /// 编辑器基类
    /// </summary>
    public abstract class EditorBase
    {
        protected const string corejs = "Doo.WebUI.SiteBuilder.PropertyEditor.Editor.js";

        protected static Hashtable editorInstances = new Hashtable();

        public static EditorBase GetEditorInstance(Type t)
        {
            EditorBase editor = editorInstances[t] as EditorBase;
            if (editor == null)
            {
                editor = Activator.CreateInstance(t) as EditorBase;
                editorInstances[t] = editor;
            }
            return editor;
        }

        /// <summary>
        /// 获取脚本资源
        /// </summary>
        /// <returns></returns>
        public virtual string ScriptResourceName { get { return corejs; } }

        /// <summary>
        /// 脚本类名
        /// </summary>
        public abstract string ClientType { get; }

        public virtual ScriptReference GetScriptReference()
        {
            return new ScriptReference(ScriptResourceName, this.GetType().Assembly.FullName);
        }

        /// <summary>
        /// 获取选项
        /// </summary>
        public virtual Hashtable Option { get { return null; } }
    }

    public class EditorHelper
    {
        protected static Hashtable WPCache = new Hashtable();

        class WPCacheItem
        {
            /// <summary>
            /// 属性名字
            /// </summary>
            public string PropertyName;

            /// <summary>
            /// 描述名字
            /// </summary>
            public string TitleName;

            /// <summary>
            /// 编辑器类型
            /// </summary>
            public Type EditorType;
        }

        //从WidgetControl创建一个编辑面板
        public static EditorPanel CreateEditorPanel<TWidget>(string id) where TWidget : WidgetControl
        {
            return CreateEditorPanel(typeof(TWidget), id);
        }

        public static EditorPanel CreateEditorPanel(Type TWidget, string id)
        {
            var ep = new EditorPanel { ID = id };
            var caneditprops = WPCache[TWidget] as WPCacheItem[];
            if (caneditprops == null)
            {
                #region 获取可编辑属性
                List<WPCacheItem> list = new List<WPCacheItem>();
                var props = TWidget.GetProperties();
                foreach (var p in props)
                {
                    var wps = p.GetCustomAttributes(typeof(WidgetPropertyAttribute), true);
                    if (wps.Length == 0) continue;
                    var wpa = wps[0] as WidgetPropertyAttribute;
                    //找到了WidgetProperty属性
                    list.Add(new WPCacheItem
                    {
                        PropertyName = p.Name,
                        TitleName = wpa.Name,
                        EditorType = wpa.EditorType
                    });
                }
                caneditprops = list.ToArray();
                WPCache[TWidget] = caneditprops;
                #endregion
            }
            foreach (var p in caneditprops)
            {
                var epitem = new EditorPanelItem { PropertyName = p.PropertyName, TitleName = p.TitleName, Editor = Activator.CreateInstance(p.EditorType) as EditorBase };
                ep.AddItem(epitem);
            }
            return ep;
        }
    }
}
